<?php
class Admin extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct()
	{
		
	}
	private function adminToObj($arrResult) {
		if(isset($arrResult[0])) {
			$objAdmin			= new stdClass();
			$objAdmin->id 		= isset($arrResult[0]['adminid']) ? $arrResult[0]['adminid'] : 0;
			$objAdmin->username = isset($arrResult[0]['username']) ? $arrResult[0]['username'] : '';
			$objAdmin->fullname = isset($arrResult[0]['fullname']) ? $arrResult[0]['fullname'] : '';
			$objAdmin->type 	= isset($arrResult[0]['type']) ? $arrResult[0]['type'] : 0;
			$objAdmin->password	= isset($arrResult[0]['password']) ? $arrResult[0]['password'] : '';
			return $objAdmin;
		}
		return null;
	}
	public function get($intAdminId) {
		try {
			$this->prepare('call admins_get(?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_STR, 30);
			$arrResult = $this->fetchAll();			
			return $this->adminToObj($arrResult);
		}catch(Exception $e) {
			Ecore_Function::logDb('Admin::get - ' .$e->getMessage());
		}		
	}
	public function getInfoByUsername($strUsername) {
		try {
			$this->prepare('call admins_get_username(?)');
			$this->bindParam(1, $strUsername, PDO::PARAM_STR, 30);
			$arrResult = $this->fetchAll();				
			return $this->adminToObj($arrResult);
		}catch(Exception $e) {			
			Ecore_Function::logDb('Admin::getInfoByUsername - ' .$e->getMessage());
		}		
	}
	
	public function getAll($intType, $intOff, $intLimit, &$intTotal=0) {
		try {
			$this->prepare('call admins_getall(?,?,?)');
			$this->bindParam(1, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();		
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;	
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Ecore_Function::logDb('Admin::getAll - ' .$e->getMessage());
		}
	}
	public function getAllCombox($intType) {
		try {
			$this->prepare('call admins_getall_com(?)');
			$this->bindParam(1, $intType, PDO::PARAM_INT, 4);;
			$arrResult 	= $this->fetchAll();					
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Ecore_Function::logDb('Admin::getAllCombox - ' .$e->getMessage());
		}
	}
	public function getPermissionByAdmin($intAdminId) {
		try {
			$this->prepare('call apermissions_get_admin(?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();	
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Ecore_Function::logDb('Admin::getPermissionByAdmin - ' .$e->getMessage());
		}
	}
	public function getPermission($intAdminId, $intSectionId) {
		try {			
			$this->prepare('call apermissions_get(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intSectionId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();	
			return isset($arrResult[0]['perm']) ? $arrResult[0]['perm'] : -1;
		} catch(Exeception $e) {
			Ecore_Function::logDb('Admin::getPermission - ' .$e->getMessage());	
		}
	}
	public function getSection() {
		try {			
			$this->prepare('call asections_getall');
			$arrResult = $this->fetchAll();
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Admin::getSection - ' .$e->getMessage());	
		}
	}
	public function add($intAdminId, $strUsername, $strPassword, $strFullname, $intType) {
		try {
			$this->prepare('call admins_add(?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strUsername, PDO::PARAM_STR, 30);
			$this->bindParam(3, $strPassword, PDO::PARAM_STR, 150);
			$this->bindParam(4, $strFullname, PDO::PARAM_STR, 150);
			$this->bindParam(5, $intType, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;			
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Admin::add - ' .$e->getMessage());		
		}
	}
	public function addAdPermission($intLoginId, $intAdminId, $intSectionId, $intPerm) {
		try {
			$this->prepare('call apermissions_add(?,?,?,?)');
			$this->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intSectionId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 6);
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;		
			if($intError == 0) {
			}			
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Admin::addAdPermission - ' .$e->getMessage());	
		}
	}
	public function edit($intLoginId, $intAdminId, $strUsername, $strPassword, $strFullname, $intType) {
		try {
			$this->prepare('call admins_update(?,?,?,?,?,?)');
			$this->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strUsername, PDO::PARAM_STR, 30);
			$this->bindParam(4, $strPassword, PDO::PARAM_STR, 150);
			$this->bindParam(5, $strFullname, PDO::PARAM_STR, 150);
			$this->bindParam(6, $intType, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Admin::edit - ' .$e->getMessage());	
		}
	}
	public function updatePassword($intLoginId, $intAdminId, $strUsername, $strPassword) {
		try {
			$this->prepare('call admins_update_password(?,?,?,?)');
			$this->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strUsername, PDO::PARAM_STR, 30);
			$this->bindParam(4, $strPassword, PDO::PARAM_STR, 150);
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Admin::updatePassword - ' .$e->getMessage());
		}
	}
	public function updateStatus($intAdminId, $strAdminId, $intPerm) {
		try {
			$this->prepare('call admins_update_status(?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strAdminId, PDO::PARAM_STR, 11);
			$this->bindParam(3, $intPerm, PDO::PARAM_INT, 6);
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;			
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Admin::updateStatus - ' .$e->getMessage());
		}
	}
	public function deleteAdPermission($intLoginId, $intAdminId) {
		try {
			$this->prepare('call apermissions_delete(?,?)');
			$this->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAdminId, PDO::PARAM_STR, 11);
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;			
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Admin::deleteAdPermission - ' .$e->getMessage());	
		}
	}
	
}
